package com.cq.hd.order.render.impl;

import com.cq.hd.common.enums.biz.CalCommissionTypeEnum;
import com.cq.hd.common.enums.biz.DefaultCommissionTypeEnum;
import com.cq.hd.common.enums.biz.OrderItemTypeEnum;
import com.cq.hd.common.response.Throw;
import com.cq.hd.order.api.dto.CreateOrderDto;
import com.cq.hd.order.api.dto.OrderCommissionDto;
import com.cq.hd.order.render.OrderContext;
import com.cq.hd.order.render.OrderRenderStepEnum;
import com.cq.hd.order.render.dto.ActiveGoodsDto;
import com.cq.hd.order.render.dto.PriceDetailDto;
import com.cq.hd.product.api.vo.ActiveVo;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author Justin.Chen
 * @since 2024/10/09 22:44
 */
@Component
public class SignFeeRecalculateRender extends AbstractRenderStep {

    @Override
    public OrderRenderStepEnum step() {
        return OrderRenderStepEnum.SIGN_FEE_RECALCULATE;
    }

    @Override
    public void render(OrderContext orderContext) {
        OrderCommissionDto commissionDto = orderContext.getCommissionDto();
        PriceDetailDto priceDetailDTO = orderContext.getPriceDetailDTO();
        CreateOrderDto createOrderDto = orderContext.getCreateOrderDto();
        // 签到费用，专属佣金如果高于基础佣金，则签到费用要扣除差值（即签到费用 = 原签到费用 - (专属佣金 - 基础佣金)）
        Integer calCommissionType = commissionDto.getCalCommissionType();
        ActiveVo activeVo = orderContext.getActiveVo();
        if (CalCommissionTypeEnum.EXCLUSIVE_COMMISSION.getValue().equals(calCommissionType)
                && activeVo.getSignInFee().compareTo(BigDecimal.ZERO) > 0) {
            BigDecimal defaultActualCommissionAmt = getDefaultActualCommissionAmt(activeVo, priceDetailDTO.getOrderPrice(), createOrderDto.getOrderNum());
            BigDecimal actualCommissionAmt = commissionDto.getActualCommissionAmt();
            BigDecimal diff = actualCommissionAmt.subtract(defaultActualCommissionAmt);
            if (diff.compareTo(BigDecimal.ZERO) > 0) {
                BigDecimal newSignInFee = activeVo.getSignInFee()
                        .subtract(diff.divide(BigDecimal.valueOf(createOrderDto.getOrderNum()), 2, RoundingMode.DOWN));
                if (newSignInFee.compareTo(BigDecimal.ZERO) < 0) {
                    newSignInFee = BigDecimal.ZERO;
                }

                activeVo.setSignInFee(newSignInFee);
                // 重新设置签到商品的签到费用
                for (ActiveGoodsDto activeGoodsDto : orderContext.getActiveGoods()) {
                    if (OrderItemTypeEnum.SIGN_IN.getValue().equals(activeGoodsDto.getType())) {
                        activeGoodsDto.setGoodsPrice(newSignInFee);
                    }
                }

                // 重新计算商品总价
                priceDetailDTO.reCalculateTotalGoodsPrice(orderContext);
            }
        }

    }

    // 活动默认佣金金额
    private BigDecimal getDefaultActualCommissionAmt(ActiveVo activeVo, BigDecimal orderPrice, Integer orderNum) {
        Integer defaultCommissionType = activeVo.getDefaultCommissionType();
        if (defaultCommissionType != null) {
            if (DefaultCommissionTypeEnum.COMMISSION.getValue().equals(defaultCommissionType)) {
                // 实际全核销佣金金额=固定佣金*下单数量
                return activeVo.getCommissionAmt().multiply(new BigDecimal(String.valueOf(orderNum)));
            } else if (DefaultCommissionTypeEnum.COMMISSION_RATE.getValue().equals(defaultCommissionType)) {
                // 实际全核销佣金金额=售价*固定佣金比例
                return orderPrice.multiply(activeVo.getCommissionRate()).divide(new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN);
            }
        }
        return BigDecimal.ZERO;
    }

}
